//
//  ByMyDocumentaryDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/23.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByMyDocumentaryDetailVC.h"
#import "ByDocumentaryDetailTitleCell.h"
#import "ByDocumentaryDetailCell.h"
#import "ByDocumentaryFourValue.h"
#import "BySuperLottoAddMinusView.h"
#import "ByDocumentaryChooseValue.h"
#import "ByFootBallBetVC.h"
#import "ByMyDocumentaryDetailTitleCell.h"
#import "ByMyDocumentaryDetailCell.h"
#import "ByMyDocumentaryOrderView.h"

#import "ByDocumentaryUserVC.h"
#import "ByBasketBallBetVC.h"

@interface ByMyDocumentaryDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByNetModel *detailModel;
@property (nonatomic, strong) BySuperLottoAddMinusView *addMinusView;


@property (nonatomic, strong) ByPayBombView *payBombView;
@property (nonatomic, strong) ByNetModel *orderModel;


@end

@implementation ByMyDocumentaryDetailVC




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"跟单方案详情";
    [self loadData];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    netModel.ID = self.netModel.ID;
    netModel.lu_id = self.netModel.lu_id;
    if (self.isMyDocumentary) {
        [ByDocumentaryApi followOrderMyPlanDetail:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            [self.tableView.mj_header endRefreshing];
            SLog(@"我发的跟单 跟单详情 = %@", result.data);
            NSDictionary *dic = result.data[@"data"];
            self.detailModel = [[ByNetModel alloc] initContentWithDic:dic[@"orderInfo"]];
            if (self.detailModel.origin_num.length > 0) {
                if (self.detailModel.lottery_type.integerValue == 1) {
                     self.detailModel.planDic = [ByFootBallHandle getFootBallGameResultWithPlan:self.detailModel.origin_num];
                }else{
                     self.detailModel.planDic = [ByBasketBallHandle getBasketBallGameResultWithPlan:self.detailModel.origin_num];
                }
               
            }
            NSMutableArray *follow_userArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *follow_userDic in dic[@"follow_user"][@"follow_user"]) {
                ByNetModel *model = [[ByNetModel alloc] initContentWithDic:follow_userDic];
                [follow_userArray addObject:model];
            }
            self.detailModel.follow_userArray = [follow_userArray copy];
            self.detailModel.amount = dic[@"follow_user"][@"amount"];
            self.detailModel.follow_num = dic[@"follow_user"][@"follow_num"];
            
            NSMutableArray *ball_infoArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *ball_infoDic in dic[@"orderInfo"][@"ball_info"]) {
                ByNetModel *model = [[ByNetModel alloc] initContentWithDic:ball_infoDic];
                model.planDic = self.detailModel.planDic;
                model.public_type = self.detailModel.public_type;
                model.is_result = self.detailModel.is_result;
                if (self.detailModel.lottery_type.integerValue == 2) {
                    NSDictionary *basketball_infoDic = ball_infoDic[@"basketball_info"];
                    model.game_Result = [ByBasketBallHandle getDocumentaryBasketBallResultWithDic:basketball_infoDic];
                }
                [ball_infoArray addObject:model];
            }
            self.detailModel.ball_infoArray = [ball_infoArray copy];
            
            [self initViews];
            
            [self.tableView reloadData];
        } failure:^(ByNetBaseResult *result) {
            [self.tableView.mj_header endRefreshing];
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }else{
        
    
        [ByDocumentaryApi followOrderFollowPlanDetail:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            [self.tableView.mj_header endRefreshing];
            SLog(@"我跟的 跟单详情 = %@", result.data);
            NSDictionary *dic = result.data[@"data"];
            self.detailModel = [[ByNetModel alloc] initContentWithDic:dic[@"plan_info"]];
            if (self.detailModel.origin_num.length > 0) {
                if (self.detailModel.lottery_type.integerValue == 1) {
                    self.detailModel.planDic = [ByFootBallHandle getFootBallGameResultWithPlan:self.detailModel.origin_num];
                }else{
                    self.detailModel.planDic = [ByBasketBallHandle getBasketBallGameResultWithPlan:self.detailModel.origin_num];
                }
                
            }
            NSMutableArray *follow_userArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *follow_userDic in dic[@"follow_user"][@"follow_user"]) {
                ByNetModel *model = [[ByNetModel alloc] initContentWithDic:follow_userDic];
                [follow_userArray addObject:model];
            }
            self.detailModel.follow_userArray = [follow_userArray copy];
            self.detailModel.follow_num = dic[@"follow_user"][@"follow_num"];
            
            NSMutableArray *ball_infoArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *ball_infoDic in dic[@"plan_info"][@"ball_info"]) {
                ByNetModel *model = [[ByNetModel alloc] initContentWithDic:ball_infoDic];
                model.planDic = self.detailModel.planDic;
                model.public_type = self.detailModel.public_type;
                model.is_result = self.detailModel.is_result;
                if (self.detailModel.lottery_type.integerValue == 2) {
                    NSDictionary *basketball_infoDic = ball_infoDic[@"basketball_info"];
                    model.game_Result = [ByBasketBallHandle getDocumentaryBasketBallResultWithDic:basketball_infoDic];
                }
                [ball_infoArray addObject:model];
            }
            self.detailModel.ball_infoArray = [ball_infoArray copy];
            
            NSDictionary *user_infoDic = dic[@"user_info"];
            self.detailModel.amount = user_infoDic[@"amount"];
            self.detailModel.commission = user_infoDic[@"commission"];
            self.detailModel.income = user_infoDic[@"income"];
            self.detailModel.rate = user_infoDic[@"rate"];
            self.detailModel.user_sn = user_infoDic[@"sn"];
            self.detailModel.create_time = user_infoDic[@"create_time"];
            [self initViews];
            
            [self.tableView reloadData];
        } failure:^(ByNetBaseResult *result) {
            [self.tableView.mj_header endRefreshing];
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
   
    
}


- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
           
            if (@available(ios 11.0,*)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
            } else {
                make.top.equalTo(self.view).offset(0);
                make.bottom.equalTo(self.view).offset(-49);
            }
        }];
        
        UIButton *betBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:betBtn];
        [betBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            
            make.top.mas_equalTo(self.tableView.mas_bottom).offset(0);
            if (@available(ios 11.0,*)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
            } else {
               make.bottom.equalTo(self.view).offset(0);
            }
        }];
        [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
        betBtn.backgroundColor = BY_THEME_COLOR;
        [betBtn setTitle:@"继续购买" forState:UIControlStateNormal];
        betBtn.tag = 200;
        
        
        [betBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        betBtn.titleLabel.font = FONT(15);
        
        [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
  
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)betAction:(UIButton *)btn{
     if (btn.tag == 200){
        
         if (self.netModel.lottery_type.integerValue == 1) {
             ByFootBallBetVC *vc = [[ByFootBallBetVC alloc] init];
             [self by_normalPush:vc hasNavi:YES];
         }else{
             ByBasketBallBetVC *vc = [[ByBasketBallBetVC alloc] init];
             [self by_normalPush:vc hasNavi:YES];
         }
      
        
    }else if (btn.tag == 300){
        
        
        self.orderModel = [[ByNetModel alloc] init];
        self.orderModel.sn = self.detailModel.sn;
        self.orderModel.money = self.detailModel.total_amount;
        self.orderModel.amount = self.detailModel.total_amount;
        self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元", self.orderModel.money];
        [self.payBombView show];
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        ByMyDocumentaryDetailTitleCell *cell = [ByMyDocumentaryDetailTitleCell cellWithTableView:tableView];
        cell.isMyDocumentary = self.isMyDocumentary;
        cell.netModel = self.detailModel;
        cell.tapViewBlock = ^(){
            ByDocumentaryUserVC *vc = [[ByDocumentaryUserVC alloc] init];
            self.detailModel.uid = self.detailModel.user_id;
            vc.netModel = self.detailModel;
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
    }else if (indexPath.row == 1){
        
        ByMyDocumentaryDetailCell *cell = [ByMyDocumentaryDetailCell cellWithTableView:tableView];
        cell.isMyDocumentary = self.isMyDocumentary;
        cell.netModel = self.detailModel;
        return cell;
        
    }else if (indexPath.row == 2){
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
        if (self.isMyDocumentary) {
        
        UIView *backgroundView = [[UIView alloc] init];
        [cell.contentView addSubview:backgroundView];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(0);
            make.right.equalTo(cell.contentView).offset(0);
            make.bottom.equalTo(cell.contentView).offset(0);
            make.top.equalTo(cell.contentView).offset(0);
        }];
        backgroundView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [backgroundView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(15);
            make.top.equalTo(backgroundView).offset(15);
            make.right.equalTo(backgroundView).offset(-10);
            make.height.mas_equalTo(@15);
        }];
        titleLabel.font = FONT(13);
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.text = [NSString stringWithFormat:@"跟单列表(%@人,%@元)",self.detailModel.follow_num,self.detailModel.amount];
        
        
        UIView *grayView = [[UIView alloc] init];
        [backgroundView addSubview:grayView];
        grayView.backgroundColor = rgba(255, 248, 249, 1);
        grayView.layer.borderColor = BY_LINE_COLOR.CGColor;
        grayView.layer.borderWidth = 1;
        
        [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(17);
            make.right.equalTo(backgroundView).offset(-17);
            make.top.equalTo(backgroundView).offset(44);
            make.bottom.equalTo(backgroundView).offset(-13);
        }];
        
        ByDocumentaryFourValue *valueView = [[ByDocumentaryFourValue alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH - 34 , 35)];
        [grayView addSubview:valueView];
        valueView.valueLabel1.text = @"用户名";
        valueView.valueLabel2.text = @"跟单金额(元)";
        valueView.valueLabel3.text = @"金额(元)";
        valueView.valueLabel4.text = @"佣金(元)";
        NSArray *dataArray = self.detailModel.follow_userArray;
        CGFloat top = 35;
        for (int i = 0; i < dataArray.count; i++){
            ByNetModel *netModel = [dataArray objectAtIndex:i];
            ByDocumentaryFourValue *valuesView = [[ByDocumentaryFourValue alloc] initWithFrame:CGRectMake(0, top, SCREEN_WIDTH - 34, 50)];
            [grayView addSubview:valuesView];
            top = top + 50;
            valuesView.valueLabel1.text = netModel.nickname;
            valuesView.valueLabel2.text = netModel.amount;
            valuesView.valueLabel3.text = netModel.prize_money;
            valuesView.valueLabel4.text = [NSString stringWithFormat:@"%@",netModel.commission];
            valueView.valueLabel1.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel2.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel3.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel4.textColor = [UIColor colorWithHexString:@"#666666"];
        }
        
        top = 0;
        for (int i = 0; i< dataArray.count + 1; i++) {
            UILabel *centerLine = [[UILabel alloc] init];
            [grayView addSubview:centerLine];
            centerLine.backgroundColor = BY_LINE_COLOR;
            if (i == 1) {
                top = top + 35;
            }else if (i > 1){
                top = top + 50;
            }
            [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(grayView).offset(0);
                make.right.equalTo(grayView).offset(0);
                make.top.equalTo(grayView).offset(top);
                make.height.mas_equalTo(@1);
            }];

        }
        
        
        
        for (int i = 1; i < 4; i++) {
            UILabel *line = [[UILabel alloc] init];
            [grayView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(grayView).offset((SCREEN_WIDTH - 30) / 4.0 * i);
                make.bottom.equalTo(grayView).offset(0);
                make.top.equalTo(grayView).offset(0);
                make.width.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
        }
            
        }
        
        return cell;
        
        
        
    }else{
        
        
        static NSString *cellIdentifier = @"cell1111";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        ByMyDocumentaryOrderView *orderView = [[ByMyDocumentaryOrderView alloc] init];
        [cell.contentView addSubview:orderView];
        [orderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(0);
            make.right.equalTo(cell.contentView).offset(0);
            make.bottom.equalTo(cell.contentView).offset(0);
            make.height.mas_equalTo(@90);
        }];
        orderView.netModel = self.detailModel;
        return cell;
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CGFloat content_height = 0;
        NSString *str = @"";
        if (_isMyDocumentary) {
            str = _detailModel.reason;
        }else{
            str = [NSString stringWithFormat:@"跟单于:%@【%@】",_detailModel.nickname,_detailModel.sn];
        }
        content_height = [str getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 32, 100) font:12].size.height + 25;
        return 60 + content_height + 75 + 50;
    }else if (indexPath.row == 1){
        
        return 43 + 35 + Documentary_Table_Height * self.detailModel.ball_infoArray.count + 23;
        
    }else if (indexPath.row == 2){
        if (self.isMyDocumentary) {
            return 43 + 35 + 50 * self.detailModel.follow_userArray.count + 13;
        }else{
            return 0.01;
        }
        
    }else{
        return 90;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
